{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit PatternEditor;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Shape, ExtCtrls, VisualUtils, superobject, Transmitter, LCLType, Buttons,
  TextPatternOptions, RegexpPatternOptions, Types, MiscUtils;

type
  TPatternEditorFrame = class(TShapeFrame)
    edtPattern: TEdit;
    edtQuality: TEdit;
    imgPattern: TImage;
    lblPercent: TLabel;
    pbxQuality: TPaintBox;
    pnlPattern: TPanel;
    pnlQuality: TPanel;
    btnOptions: TSpeedButton;
    procedure btnOptionsClick(Sender: TObject);
    procedure edtPatternChange(Sender: TObject);
    procedure edtQualityChange(Sender: TObject);
    procedure edtQualityExit(Sender: TObject);
    procedure edtQualityKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ControlEnter(Sender: TObject);
    procedure pbxQualityPaint(Sender: TObject);
  private
    FOptionsImageName: String;
    FQualityPercent: Integer;
    procedure DisplayQualityPercent;
    { private declarations }
  public
    procedure AfterConstruction; override;
    procedure Render;
    { public declarations }
  end;
  TPatternEditorFrameList = TGenericObjectList<TPatternEditorFrame>;

implementation

{$R *.lfm}
{$R *.rc}

resourcestring
  SQuality = 'Correctness';
  STextPattern = 'Text Pattern';
  SRegexpPattern = 'Regular Expression';

const
  TEXT_PATTERN_KIND = $9a6b61abf6224453;
  REGEXP_PATTERN_KIND = $c43a1fae511d751a;

{ TPatternEditorFrame }

procedure TPatternEditorFrame.edtPatternChange(Sender: TObject);
var
  Input: ISuperObject;
begin
  Input := SO;
  Input.S['value'] := edtPattern.Text;
  Dispatcher.PerformAction(Id, 'setValue', Input);
end;

procedure TPatternEditorFrame.btnOptionsClick(Sender: TObject);
begin
  case Scene.GetKind(Shape) of
    TEXT_PATTERN_KIND: TTextPatternOptionsForm.OpenModal(Id, Shape.Clone, Dispatcher);
    REGEXP_PATTERN_KIND: TRegexpPatternOptionsForm.OpenModal(Id, Shape.Clone, Dispatcher);
    else
      Assert( FALSE );
  end;
end;

procedure TPatternEditorFrame.edtQualityChange(Sender: TObject);
var
  Input: ISuperObject;
  Ok: Boolean;
begin
  Ok := TRUE;
  Input := SO;
  Input.S['quality'] := edtQuality.Text;
  try
    Dispatcher.PerformAction(Id, 'setQuality', Input);
  except on ECommandFailed do
    Ok := FALSE;
  end;

  if Ok then
    ResetEditError(edtQuality)
  else
    SetEditError(edtQuality);
end;

procedure TPatternEditorFrame.edtQualityExit(Sender: TObject);
begin
  DisplayQualityPercent;
end;

procedure TPatternEditorFrame.edtQualityKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_ESCAPE) and (Shift = []) then
  begin
    DisplayQualityPercent;
    edtQuality.SelectAll;
  end;
end;

procedure TPatternEditorFrame.ControlEnter(Sender: TObject);
begin
  if not (Sender as TControl).MouseEntered then
    ScrollControlIntoView(Sender as TControl);
end;

procedure TPatternEditorFrame.pbxQualityPaint(Sender: TObject);
var
  c: TCanvas;
  r: TRect;
begin
  c := pbxQuality.Canvas;
  c.Pen.Color := $808080;
  r := pbxQuality.ClientRect;
  c.Rectangle(r);

  InflateRect(r, -1, -1);
  r.Right := r.Left + Round((r.Right - r.Left) / 100 * FQualityPercent);
  c.Brush.Color := $2aff7f;
  c.FillRect(r);
end;

procedure TPatternEditorFrame.DisplayQualityPercent;
begin
  SetEditTextWithoutOnChange(edtQuality, IntToStr(FQualityPercent));
  ResetEditError(edtQuality);
end;

procedure TPatternEditorFrame.AfterConstruction;
begin
  inherited;
  edtPattern.Font.Name := 'Courier New';
  edtPattern.Font.Size := 12;
  edtPattern.MaxLength := Shape.I['maxSize'];
  SetEditWidthByMaxLength(edtQuality);
  pbxQuality.Height := Abs(Screen.SystemFont.Height) div 2 + 2;
  edtQuality.Hint := SQuality;
  lblPercent.Hint := SQuality;
  pbxQuality.Hint := SQuality;

  case Scene.GetKind(Shape) of
    TEXT_PATTERN_KIND:
      begin
        imgPattern.Hint := STextPattern;
        LoadPngResourceToPicture('PatternEditor/textPattern.png', imgPattern.Picture);
      end;
    REGEXP_PATTERN_KIND:
      begin
        imgPattern.Hint := SRegexpPattern;
        LoadPngResourceToPicture('PatternEditor/regexpPattern.png', imgPattern.Picture);
      end;
    else
      Assert( FALSE );
  end;
end;

procedure TPatternEditorFrame.Render;
var
  s: String;
begin
  UpdateShape;

  SetEditTextWithoutOnChange(edtPattern, Shape.S['value']);
  FQualityPercent := Shape.I['qualityPercent'];
  if not edtQuality.Focused then
    DisplayQualityPercent;
  pbxQuality.Invalidate;

  if Shape.B['hasDefaultOptions'] then
    s := 'PatternEditor/options.png'
  else
    s := 'PatternEditor/customOptions.png';
  if FOptionsImageName <> s then
  begin
    FOptionsImageName := s;
    LoadPngResourceToBitmap(FOptionsImageName, btnOptions.Glyph);
  end;
end;

end.

